package com.guldan.jaina.task;

import com.guldan.core.enums.CitizenUpdateTypeEn;
import com.guldan.jaina.dto.citizenbatchupdate.CitizenBatchUpdateDTO;
import com.guldan.jaina.dto.progress.BarDTO;
import com.guldan.jaina.dto.users.UsersDTO;
import com.guldan.jaina.manager.CitizenBatchUpdateManager;
import com.guldan.jaina.utils.UserUtils;
import com.guldan.mapper.db.entity.CitizenBatchUpdate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by Silas.
 * Date: 2016/12/26
 * Time: 22:32
 */
public class CitizenUpdateThread implements Runnable {

    public Logger logger = LoggerFactory.getLogger(getClass());

    private CitizenBatchUpdateManager citizenBatchUpdateManager;

    private Integer updateType;

    private String threadName;

    private List<CitizenBatchUpdateDTO> data;

    private BarDTO barDTO;

    private UsersDTO usersDTO;

    public void setBarDTO(BarDTO barDTO) {
        this.barDTO = barDTO;
    }

    public void setThreadName(String threadName) {
        this.threadName = threadName;
    }

    public void setData(List<CitizenBatchUpdateDTO> data) {
        this.data = data;
    }

    public void setUpdateType(Integer updateType) {
        this.updateType = updateType;
    }

    public void setCitizenBatchUpdateManager(CitizenBatchUpdateManager citizenBatchUpdateManager) {
        this.citizenBatchUpdateManager = citizenBatchUpdateManager;
    }

    public void setUsersDTO(UsersDTO usersDTO) {
        this.usersDTO = usersDTO;
    }

    @Override
    public void run() {
        UserUtils.setUser(this.usersDTO);


        logger.info(this.threadName + " begin, this.data.size " + this.data.size());
        long cost = System.currentTimeMillis();

        for (CitizenBatchUpdateDTO dto : this.data) {
            if (this.updateType.equals(CitizenUpdateTypeEn.citizen_name.getCode())) {
                this.barDTO.finishAdd(updateName(dto));
            } else if (this.updateType.equals(CitizenUpdateTypeEn.citizen_bank.getCode())) {
                this.barDTO.finishAdd(updateBank(dto));
            } else {
                this.barDTO.finishAdd(updateArea(dto));

            }
        }
        logger.info(this.threadName + " cost " + (System.currentTimeMillis() - cost));
    }

    private Boolean updateBank(CitizenBatchUpdateDTO dto) {
        return citizenBatchUpdateManager.batchUpdateBank(dto);
    }

    private Boolean updateName(CitizenBatchUpdateDTO dto) {
        return citizenBatchUpdateManager.batchUpdateName(dto);
    }

    private Boolean updateArea(CitizenBatchUpdateDTO dto) {
        return citizenBatchUpdateManager.batchUpdateArea(dto);
    }
}
